//index.js
//获取应用实例
const app = getApp()
const api = require("../../utils/api.js")
console.log(api)
Page({

  onShow() {
    // 	 	读取函数
    var that = this;
    let token=app.globalData.token
    if (token==undefined){
      wx.showToast({
        title: '您没有登录',
        icon: 'success',
        duration: 20000
      });
    }
      wx.request({
        url: api.shops,
        header: {
          'X-Nideshop-Token': token
        },
        success(res) {
          console.log("获取的数据", res)
          that.setData({
              cartList: res.data.data.cartList,
            cartTotal: res.data.data.cartTotal
            },
          );
        }
      })
  },
  data: {
    cartList: [],
    cartTotal:[],
  },

  /**
   * 用户点击商品加1
   */
  /**
   * 用户点击商品减1
   */
  subtracttap: function(e) {
    let {
      index
    } = e.target.dataset;
    var cartList = this.data.cartList;
    var numbers = cartList[index].number;
    console.log(cartList)
    if (numbers <= 1) {
      return;
    } else {
      cartList[index].number--;
      this.setData({
        index,
        'cartList': cartList
      });
    }
  },
  addtap: function(e) {
    let {
      index
    } = e.target.dataset;
    var cartList = this.data.cartList;
    var numbers = cartList[index].number;
    console.log(cartList)
    cartList[index].number++;
    this.setData({
      index,
      'cartList': cartList
    })
  },
  // 单选
  checkboxs(e) {
    let token = app.globalData.token
    if (token == undefined) {
      wx.showToast({
        title: '您没有登录',
        icon: 'success',
        duration: 20000
      });
    }
    var that = this;
    const index = e.currentTarget.dataset.index; // 获取data- 传进来的index
    let cartList = that.data.cartList; // 获取购物车列表
    console.log(cartList)
    let isChecked = cartList[index].checked; // 获取当前商品的选中状态
    let productId = cartList[index].product_id;//获取点击产品的id
    console.log(isChecked)
    if (isChecked==0){
      isChecked=1
    }else{
      isChecked=0
    }
      wx.request({
        url: api.checked,
        method:'POST',
        data:{
          productIds: productId,
          isChecked: isChecked
        },
        header: {
          'X-Nideshop-Token': token
        },
        success(res) {
          console.log(res)
          that.setData({
            cartList: res.data.data.cartList,
            cartTotal:res.data.data.cartTotal
          });
        }
      })
      
  },
  checkboxChange: function(e) {
    console.log('checkbox发生change事件，携带value值为：', e.target);
    var checkboxItems = this.data.cartList;
    for (var i = 0; i < checkboxItems.length; ++i) {
      checkboxItems[i].checked =1;
      console.log(checkboxItems[i].checked)
    }
    console.log("checkboxItems", checkboxItems)
    this.setData({
      cartList: checkboxItems
    })
  },
  // 删除
  deleteShop(event){
    var that = this;
    wx.showToast({
      title: '删除成功',
      icon: 'success',
      duration: 2000
    });
    console.log("删除的数据",event)
   let index = event.currentTarget.dataset.index;
    console.log("下标", index)
    var productIds = String(that.data.cartList[index].product_id) ;

    console.log(productIds)
    let token = app.globalData.token
    if (token == undefined) {
      wx.showToast({
        title: '您没有登录',
        icon: 'success',
        duration: 20000
      });
    }
    wx.request({
      url: api.deleteShop,
      method: 'POST',
      data: {
        productIds: productIds
      },
      header: {
        'X-Nideshop-Token': token
      },
      success(res) {
        console.log(res)
        that.setData({
          cartList: res.data.data.cartList
        })
      }
    })
  }

})